package org.ocean.online.web;

import java.io.File;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
import org.ocean.hibernate.BaseHibernateDao;
import org.ocean.online.constant.Constant;
import org.ocean.online.sys.BusinessException;
import org.ocean.online.sys.OnlineConfig;
import org.ocean.online.user.initdb.InitUserAction;
import org.ocean.online.utils.ParamUtil;
import org.ocean.online.wicket.BaseWebPage;
import org.ocean.online.wicket.NeedLoginPage;

public class Registe extends BaseWebPage{

	private static final long serialVersionUID = -1861892225635535814L;

	public Registe(){
		Form<?> form = new Form("form");
		AjaxSubmitLink btn = new AjaxSubmitLink("ajaxSubmitBtn") {

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				try{
					Map<String, String> data = ParamUtil.toMap(form.getRequest().getPostParameters());
					validate(data);
					if(isUserExists(data.get("username"))){
						target.appendJavaScript("alert('用户名"+data.get("username")+"已经存在');");
						return;
					}
					data.put(Constant.Workspace_VariableName, OnlineConfig.getProperty("onlinedata_dir")+File.separator+data.get("username"));
					Map<String, Object> user = addUser(data);
					initWorkspace(user);
					HttpServletRequest servletRequest = ((ServletWebRequest) getRequest()).getContainerRequest();
					servletRequest.getSession().setAttribute("user",user);
					target.appendJavaScript("alert('注册成功')");
//					target.getPage().continueToOriginalDestination();
					target.appendJavaScript("window.location='workspace2'");
				}catch(BusinessException ex){
					target.appendJavaScript("alert('"+ex.getMessage()+"')");
				}
				catch(Exception ex){
					log.error("registe exception",ex);
					target.appendJavaScript("alert('system exception when registe')");
				}
			}
			
		};
		form.add(btn);
		
		add(form);
	}
	
	private boolean isUserExists(String username){
		BaseHibernateDao dao = new BaseHibernateDao();
		long count = dao.countHqlResult("from user where username = ?", new Object[]{username});
		if(count>0){
			return true;
		}
		return false;
	}
	
	private Map addUser(Map<String,String> user){
		BaseHibernateDao dao = new BaseHibernateDao();
		//SQLQuery query = dao.getSession().createSQLQuery("create DATABASE "+user.get("username")+" CHARSET 'utf8'");
		//query.executeUpdate();
//		String dbfile="e:\\dbspace\\"+user.get("username")+".mdb";
//		try {
//			File db = new File(dbfile);
//			if(db.exists()==false){
//				Database.create(new File(dbfile));
//			}
//		} catch (IOException e) {
//			throw new BusinessException("create database file fail",e);
//		}
		dao.save("user", user);
		return user;
	}
	
	private void validate(Map<String,String> user){
		String password = user.get("password");
		String pwdagain = user.get("pwdagain");
		if(password==null || pwdagain==null){
			throw new BusinessException("密码或确认密码不能为空");
		}
		if(!password.equals(pwdagain)){
			throw new BusinessException("两次输入密码不一样");
		}
	}
	
	private void initWorkspace(Map user){
		InitUserAction.init(user);
	}
}
